package com.news.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.bean.NewsType;
import com.news.dao.NewsTypeDao;
import com.news.dao.impl.NewsTypeDaoImpl;

public class NewsTypeServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        NewsType nt = new NewsType();
        NewsTypeDao dao = new NewsTypeDaoImpl();
        String cmd = req.getParameter("cmd");
        String id = req.getParameter("id");//("id");//("id");
        String name = req.getParameter("name");
        String sort = req.getParameter("sort");//("sort");
        //System.out.println("cmd"+"=="+cmd+"id"+"=="+id+"name"+"=="+name+"sort"+"=="+sort);
        if (id != null) {
            nt.setId(Integer.valueOf(id));
        }
        nt.setName(name);
        if (sort != null) {
            nt.setSort(Integer.valueOf(sort));
        }
        if (cmd != null) {
            if (cmd.equals("list")) {
                List<NewsType> list = dao.list(new HashMap());
                req.setAttribute("list", list);
                req.getRequestDispatcher("newsTypeList.jsp").forward(req,
                        resp);
            } else if (cmd.equals("add")) {
                dao.add(nt);
                resp.sendRedirect("newstype?cmd=list");
            } else if (cmd.equals("loadedit")) {
                dao.get(Integer.valueOf(id));
                System.out.println("-----------------loadedit successful-------------");
                System.out.println("nt.name" + nt.getName());
                System.out.println("nt.sort" + nt.getSort());
                System.out.println("nt.id" + nt.getId());
                req.setAttribute("newsType", nt);
                req.getRequestDispatcher("newsTypeEdit.jsp").forward(req, resp);
            } else if (cmd.equals("edit")) {
                dao.edit(nt);
                System.out.println("cmd.equals('edit')" + "==" + cmd.equals("edit"));
                System.out.println("dao.edit(nt)" + "==" + dao.edit(nt));
                //req.getSession().setAttribute("succ", "1");
                resp.sendRedirect("newstype?cmd=list");
            } else if (cmd.equals("delete")) {
                int result = dao.del(Integer.valueOf(id));
                if (result == 0) {
                    System.out.println("ppppppppppppppppppppppppp");
                    resp.sendRedirect("newstype?cmd=list&err=foreignErr");
                } else {
                    req.getRequestDispatcher("newstype?cmd=list").forward(req, resp);
                }
            } else if (cmd.equals("sort")) {
                HashMap map = new HashMap();
                List<NewsType> list = (List) dao.list(map);
                req.setAttribute("list", list);
                req.getRequestDispatcher("news?cmd=loadedit").forward(req, resp);
            }
        }
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
